-- Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
--
-- this file licensed under the Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
--
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
-- PURPOSE.
-- See the Mulan PSL v2 for more details.
--
-- Author: auto generate
-- Create: 2025-5-7
-- Description: DO NOT EDIT; Code generated by "test_${project_name}.lua.mako"

local skynet = require 'skynet'
local log = require 'mc.logging'
local utils = require 'mc.utils'
local sdbus = require 'sd_bus'
local test_common = require 'test_common.utils'
local const = require 'constants'
require 'skynet.manager'

local project_dir = os.getenv('PROJECT_DIR')
local test_dir = project_dir .. '/test/integration/.test_temp_data/'
skynet.setenv('PROG_APP_PATH', test_dir .. 'apps')
skynet.setenv('PROG_CSR_PATH', test_dir .. 'sr')
skynet.setenv('DATA_CSR_PATH', test_dir .. 'data/sr')
skynet.setenv('DUMP_FILE_PATH', test_dir .. 'dump')

log:setLevel(log.INFO)

local function prepare_test_data()
    log:info('prepare test data')
    local test_data_dir = skynet.getenv('TEST_DATA_DIR')
    os.execute('mkdir -p ' .. test_data_dir)
    os.execute('mkdir -p ' .. '/tmp/test_dump')

    -- prepare folders
    local dir_list = {'apps/active_standby_mgmt/mds', 'apps/hwdiscovery/mds', 'sr', 'dump', 'data',
        'apps/hwproxy/mds', 'usr/lib64'}
    for _, path in pairs(dir_list) do
        os.execute('mkdir -p ' .. test_data_dir .. '/' .. path)
    end

    -- libsoc_adapter_it.so中路径固定使用test/integration
    os.execute('mkdir -p test/integration/.test_temp_data/data')
    os.execute('cp' .. ' -r ' .. 'test/integration/test_data/mockdata' .. ' ' .. test_data_dir .. 'data')
    utils.copy_file('temp/usr/lib64/mock/libsoc_adapter_it.so',
        test_data_dir .. '/usr/lib64/libsoc_adapter.so')

    -- prepare mds
    utils.copy_file('temp/opt/bmc/apps/hwdiscovery/mds/schema.json',
        test_data_dir .. '/apps/hwdiscovery/mds/schema.json')
    utils.copy_file('temp/opt/bmc/apps/hwproxy/mds/schema.json',
        test_data_dir .. '/apps/hwproxy/mds/schema.json')
    utils.copy_file('mds/schema.json', test_data_dir .. '/apps/active_standby_mgmt/mds/schema.json')

    -- prepare sr
    utils.copy_file('test/integration/test_data/root.sr', test_data_dir .. '/sr/root.sr')
    utils.copy_file('test/integration/test_data/platform.sr', test_data_dir .. '/sr/platform.sr')
end

local function clear_test_data(exit_test)
    log:info('clear test data')
    local test_data_dir = skynet.getenv('TEST_DATA_DIR')
    if not exit_test then
        return utils.remove_file(test_data_dir)
    end

    skynet.timeout(0, function()
        skynet.abort()
        utils.remove_file(test_data_dir)
        utils.remove_file('/tmp/test_dump')
    end)
end

local function test_start()
    log:info('================ test start ================')
    local bus = sdbus.open_user(true)
    bus:start()
    skynet.sleep(400)  -- 等待4s(400)对象分发
    require 'test_entry'.test_entry(bus)
    log:info('================ test complete ================')
end

skynet.start(function()
    clear_test_data(false)
    prepare_test_data()
    test_common.dbus_launch()
    skynet.uniqueservice('sd_bus')
    skynet.uniqueservice('persistence/service/main')
    skynet.uniqueservice('maca/service/main')
    skynet.uniqueservice('hwdiscovery/service/main')
    skynet.uniqueservice('hwproxy/service/main')
    skynet.uniqueservice('main')
    skynet.fork(function()
        local ok, err = pcall(test_start)
        clear_test_data(true)
        if not ok then
            error(err)
        end
    end)
end)
